<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.template &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="django" href="../django.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-template">
            
  <h1>Source code for django.template</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This is the Django template system.</span>

<span class="sd">How it works:</span>

<span class="sd">The Lexer.tokenize() function converts a template string (i.e., a string containing</span>
<span class="sd">markup with custom template tags) to tokens, which can be either plain text</span>
<span class="sd">(TOKEN_TEXT), variables (TOKEN_VAR) or block statements (TOKEN_BLOCK).</span>

<span class="sd">The Parser() class takes a list of tokens in its constructor, and its parse()</span>
<span class="sd">method returns a compiled template -- which is, under the hood, a list of</span>
<span class="sd">Node objects.</span>

<span class="sd">Each Node is responsible for creating some sort of output -- e.g. simple text</span>
<span class="sd">(TextNode), variable values in a given context (VariableNode), results of basic</span>
<span class="sd">logic (IfNode), results of looping (ForNode), or anything else. The core Node</span>
<span class="sd">types are TextNode, VariableNode, IfNode and ForNode, but plugin modules can</span>
<span class="sd">define their own custom node types.</span>

<span class="sd">Each Node has a render() method, which takes a Context and returns a string of</span>
<span class="sd">the rendered node. For example, the render() method of a Variable Node returns</span>
<span class="sd">the variable&#39;s value as a string. The render() method of an IfNode returns the</span>
<span class="sd">rendered output of whatever was inside the loop, recursively.</span>

<span class="sd">The Template class is a convenient wrapper that takes care of template</span>
<span class="sd">compilation and rendering.</span>

<span class="sd">Usage:</span>

<span class="sd">The only thing you should ever use directly in this file is the Template class.</span>
<span class="sd">Create a compiled template object with a template_string, then call render()</span>
<span class="sd">with a context. In the compilation stage, the TemplateSyntaxError exception</span>
<span class="sd">will be raised if the template doesn&#39;t have proper syntax.</span>

<span class="sd">Sample code:</span>

<span class="sd">&gt;&gt;&gt; from django import template</span>
<span class="sd">&gt;&gt;&gt; s = u&#39;&lt;html&gt;{% if test %}&lt;h1&gt;{{ varvalue }}&lt;/h1&gt;{% endif %}&lt;/html&gt;&#39;</span>
<span class="sd">&gt;&gt;&gt; t = template.Template(s)</span>

<span class="sd">(t is now a compiled template, and its render() method can be called multiple</span>
<span class="sd">times with multiple contexts)</span>

<span class="sd">&gt;&gt;&gt; c = template.Context({&#39;test&#39;:True, &#39;varvalue&#39;: &#39;Hello&#39;})</span>
<span class="sd">&gt;&gt;&gt; t.render(c)</span>
<span class="sd">u&#39;&lt;html&gt;&lt;h1&gt;Hello&lt;/h1&gt;&lt;/html&gt;&#39;</span>
<span class="sd">&gt;&gt;&gt; c = template.Context({&#39;test&#39;:False, &#39;varvalue&#39;: &#39;Hello&#39;})</span>
<span class="sd">&gt;&gt;&gt; t.render(c)</span>
<span class="sd">u&#39;&lt;html&gt;&lt;/html&gt;&#39;</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="c"># Template lexing symbols</span>
<span class="kn">from</span> <span class="nn">django.template.base</span> <span class="kn">import</span> <span class="p">(</span><span class="n">ALLOWED_VARIABLE_CHARS</span><span class="p">,</span> <span class="n">BLOCK_TAG_END</span><span class="p">,</span>  <span class="c"># NOQA</span>
    <span class="n">BLOCK_TAG_START</span><span class="p">,</span> <span class="n">COMMENT_TAG_END</span><span class="p">,</span> <span class="n">COMMENT_TAG_START</span><span class="p">,</span>
    <span class="n">FILTER_ARGUMENT_SEPARATOR</span><span class="p">,</span> <span class="n">FILTER_SEPARATOR</span><span class="p">,</span> <span class="n">SINGLE_BRACE_END</span><span class="p">,</span>
    <span class="n">SINGLE_BRACE_START</span><span class="p">,</span> <span class="n">TOKEN_BLOCK</span><span class="p">,</span> <span class="n">TOKEN_COMMENT</span><span class="p">,</span> <span class="n">TOKEN_TEXT</span><span class="p">,</span> <span class="n">TOKEN_VAR</span><span class="p">,</span>
    <span class="n">TRANSLATOR_COMMENT_MARK</span><span class="p">,</span> <span class="n">UNKNOWN_SOURCE</span><span class="p">,</span> <span class="n">VARIABLE_ATTRIBUTE_SEPARATOR</span><span class="p">,</span>
    <span class="n">VARIABLE_TAG_END</span><span class="p">,</span> <span class="n">VARIABLE_TAG_START</span><span class="p">,</span> <span class="n">filter_re</span><span class="p">,</span> <span class="n">tag_re</span><span class="p">)</span>

<span class="c"># Exceptions</span>
<span class="kn">from</span> <span class="nn">django.template.base</span> <span class="kn">import</span> <span class="p">(</span><span class="n">ContextPopException</span><span class="p">,</span> <span class="n">InvalidTemplateLibrary</span><span class="p">,</span>  <span class="c"># NOQA</span>
    <span class="n">TemplateDoesNotExist</span><span class="p">,</span> <span class="n">TemplateEncodingError</span><span class="p">,</span> <span class="n">TemplateSyntaxError</span><span class="p">,</span>
    <span class="n">VariableDoesNotExist</span><span class="p">)</span>

<span class="c"># Template parts</span>
<span class="kn">from</span> <span class="nn">django.template.base</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Context</span><span class="p">,</span> <span class="n">FilterExpression</span><span class="p">,</span> <span class="n">Lexer</span><span class="p">,</span> <span class="n">Node</span><span class="p">,</span>  <span class="c"># NOQA</span>
    <span class="n">NodeList</span><span class="p">,</span> <span class="n">Parser</span><span class="p">,</span> <span class="n">RequestContext</span><span class="p">,</span> <span class="n">Origin</span><span class="p">,</span> <span class="n">StringOrigin</span><span class="p">,</span> <span class="n">Template</span><span class="p">,</span>
    <span class="n">TextNode</span><span class="p">,</span> <span class="n">Token</span><span class="p">,</span> <span class="n">TokenParser</span><span class="p">,</span> <span class="n">Variable</span><span class="p">,</span> <span class="n">VariableNode</span><span class="p">,</span> <span class="n">constant_string</span><span class="p">,</span>
    <span class="n">filter_raw_string</span><span class="p">)</span>

<span class="c"># Compiling templates</span>
<span class="kn">from</span> <span class="nn">django.template.base</span> <span class="kn">import</span> <span class="p">(</span><span class="n">compile_string</span><span class="p">,</span> <span class="n">resolve_variable</span><span class="p">,</span>  <span class="c"># NOQA</span>
    <span class="n">unescape_string_literal</span><span class="p">,</span> <span class="n">generic_tag_compiler</span><span class="p">)</span>

<span class="c"># Library management</span>
<span class="kn">from</span> <span class="nn">django.template.base</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Library</span><span class="p">,</span> <span class="n">add_to_builtins</span><span class="p">,</span> <span class="n">builtins</span><span class="p">,</span>  <span class="c"># NOQA</span>
    <span class="n">get_library</span><span class="p">,</span> <span class="n">get_templatetags_modules</span><span class="p">,</span> <span class="n">get_text_list</span><span class="p">,</span> <span class="n">import_library</span><span class="p">,</span>
    <span class="n">libraries</span><span class="p">)</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Template&#39;</span><span class="p">,</span> <span class="s">&#39;Context&#39;</span><span class="p">,</span> <span class="s">&#39;RequestContext&#39;</span><span class="p">,</span> <span class="s">&#39;compile_string&#39;</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">Module code</a>
        
          <ul><li><a href="../django.html">django</a>
        
        <ul><li>django.template</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>